﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

namespace UI.Models
{
    public class GameDisplay
    {
        public int GameId { get; set; }

        [Display(Name = "Game Date")]
        [DisplayFormat(NullDisplayText = "", ApplyFormatInEditMode = true, DataFormatString = "{0:dd MMMM yyyy}")]
        public DateTime GameDate { get; set; }

        [Display(Name = "Game Time")]
        public string GameTime { get; set; }

        [Display(Name = "Currently Playing")]
        public ICollection<Domain.Player> Playing { get; set; }

        [Display(Name = "Currently Substituting")]
        public ICollection<Domain.Player> Substitutions { get; set; }

        [Display(Name = "Status")]
        public string GameStatus { get; set; }

        [Display(Name = "Error")]
        public string Error { get; set; }

        
        public GameDisplay()
        {
            Playing = new List<Domain.Player>();
            Substitutions = new List<Domain.Player>();            
        }

        public DateTime GetGameDateAndTime()
        {
            int hrs;
            Int32.TryParse(GameTime.Substring(0, 2), out hrs);
            int minutes;
            Int32.TryParse(GameTime.Substring(GameTime.IndexOf(':') + 1, 2), out minutes);
            return GameDate.AddHours(hrs).AddMinutes(minutes);
        }

        public static GameDisplay MapToUI(Domain.Game game)
        {
            return new GameDisplay
            {
                GameId = game.GameId,
                GameDate = game.GameDateAndTime.Date,
                GameTime = game.GameDateAndTime.Hour.ToString() + ":" + game.GameDateAndTime.Minute.ToString(),
                GameStatus = game.GameStatus.ToString(),
                Playing = game.GetPlayingPlayers(),
                Substitutions = game.GetSubstitutingPlayers()
            };
        }
    }


}